var estimateReport = new Object();
estimateReport.page;
estimateReport.page_size=10;
// 01单选题 02.多选题 03.判断题
var questionType = {
    "01":"单选题",
    "02":"多选题",
    "03":"判断题",
    "05":"简答题"
};
estimateReport.init = function (_page) {
    estimateReport.page = _page;
    estimateReport.questionReport();
    estimateReport.studentReport();
    estimateReport.changeTab();

};

// 加载题目评估结果
estimateReport.questionReport = function () {
    var params = {
        'projectId':$("#projectId").val(),
        'templateId': $("#templateId").val(),
        'projectType':$("#projectType").val()
    };
    $.ajax({
        url: appPath + "/admin/projectEstimate/questionReport",
        data: params,
        async: false,
        type: "post",
        dataType:"json",
        success:function(data){
            var result = data.result;
            var html = '', item, options, option, users, user,answer;
            for(var i=0;i<result.length;i++){
                item = result[i];
                html += '<div class="questionDiv">';
                html += '<h3>题目' + (i+1) + '：'+item.varQuestionsContent+'（'+questionType[item.chrType]+'）</h3>';
                // 选择题
                if(item.hasOwnProperty('options')){
                    html += '<table class="questionTable">';
                    html += '<tr>';
                    html += '<td>选项</td>';
                    html += '<td>选项内容</td>';
                    html += '<td>选择人员数量</td>';
                    html += '<td>选择人员比例</td>';
                    html += '</tr>';
                    options = item.options;
                    for(var j=0;j<options.length;j++){
                        option = options[j];
                        html += '<tr>';
                        html += '<td>'+option.oType+'</td>';
                        html += '<td>'+option.optionContent+'</td>';
                        html += '<td>'+option.chooseCount+'</td>';
                        html += '<td>';
                        html += option.chooseCount == 0 ? 0 : (option.chooseCount*100.0/item.countUser).toFixed(2);
                        html += '%</td>';
                        html += '</tr>';
                    }
                    html += '</table>';
                }
                // 简答题
                if(item.hasOwnProperty('users')){
                    html += '<table class="questionTable">';
                    html += '<tr>';
                    html += '<td>学员姓名</td>';
                    html += '<td>单位</td>';
                    html += '<td>部门</td>';
                    html += '<td width="30%">答题记录</td>';
                    html += '</tr>';
                    users = item.users;
                    for(var j=0;j<users.length;j++){
                        user = users[j];
                        html += '<tr>';
                        html += '<td>'+user.userName+'</td>';
                        html += '<td>'+user.companyName+'</td>';
                        html += '<td>'+user.deptName+'</td>';
                        answer = user.answer.length > 44 ? user.answer.substr(0,43).concat("....") : user.answer;
                        html += '<td title="'+user.answer+'">'+answer+'</td>'
                        // html += '<td><a class="btn btn-info" href="javascript:estimateReport.answerRecord(\''+user.userId+'\');">作答记录</a></td>';
                        html += '</tr>';
                    }
                    html += '</table>';
                }

                html += '</div>';
            }
            $("#project_div").html(html);
        }
    })
};

// 加载人员评估结果
estimateReport.studentReport = function () {
    var list_url = appPath + "/admin/projectEstimate/userReport";
    estimateReport.page.init("user_form", list_url, "user_table", "user_page", 1, estimateReport.page_size);
    estimateReport.page.goPage(1);
    estimateReport.page.list = function(dataList){
        var html = "", item;
        var len = dataList.length;
        for(var i=0; i< len; i++) {
            item = dataList[i];
            html += '<tr>';
            html += '<td>'+item.user_name+'</td>';
            html += '<td>'+item.user_account+'</td>';
            html += '<td>'+item.company_name+'</td>';
            html += '<td>'+item.dept_name+'</td>';
            html += '<td>';
            html += $("#answerType").val() == "1" ? '<a class="btn btn-info" href="javascript:estimateReport.answerRecord(\''+item.user_id+'\');">作答记录</a>' : '\\';
            html += '</td>';
            html += '</tr>';
        }
        return html;
    }
};

// 保存管理员评价
estimateReport.saveAppraise = function () {
    var content = UE.getEditor('content').getContent();
    $.ajax({
        url: appPath + "/admin/projectEstimate/saveAppraise",
        data: {'projectId': $("#projectId").val(), 'content':content},
        async: false,
        type: "post",
        dataType:"json",
        success:function(data){
        }
    });
};

// tab类型切换
estimateReport.changeTab = function () {
    $("#click-report-type").on("click", "span", function(){
        $('#click-report-type').find("span").removeClass("active");
        $(this).addClass("active");
        var value = $(this).attr("data-value");
        if('project' == value){
            $(".title").html('学员评估结果');
            $("#project_div").show();
            $("#user_div").hide();
        }else if('user' == value){
            $(".title").html('学员评估汇总');
            $("#user_div").show();
            $("#project_div").hide();
        }
    });

    $("#click-user-type").on("click", "span", function(){
        $('#click-user-type').find("span").removeClass("active");
        $(this).addClass("active");
        var value = $(this).attr("data-value");
        if('admin' == value){
            $(".title").html('管理员评估结果');
            $("#admin_div").show();
            $("#btn_save").show();
            $("#project_div").hide();
            $("#user_div").hide();
            $("#reportType").hide();
        }else if('student' == value){
            $(".title").html('学员评估结果');
            $("#admin_div").hide();
            $("#btn_save").hide();
            $("#reportType").show();
            $('#click-report-type').find("span").removeClass("active").eq(0).addClass("active");
            $("#project_div").show();
            $("#user_div").hide();
        }
    });
};


// 学员答题记录
estimateReport.answerRecord = function (userId) {
    window.open("/admin/projectEstimate/answerRecord?projectId="+$("#projectId").val()+"&templateId="+$("#templateId").val()+"&userId="+userId, "_blank");

};